﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Windows.Forms;


namespace FB
{
    /// <summary>
    /// Summary description for CustomBrowser
    /// </summary>
    public class CustomBrowser
    {
        public CustomBrowser()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        protected string _url;
        public string html = "";
        public string GetWebpage(string url)
        {
            _url = url;
            // WebBrowser is an ActiveX control that must be run in a
            // single-threaded apartment so create a thread to create the
            // control and generate the thumbnail
            //Thread thread = new Thread(new ThreadStart(GetWebPageWorker));
            //thread.SetApartmentState(ApartmentState.STA);
            //thread.Start();
            //thread.Join();
            //string s = html;
            //return s;
            Thread thread = new Thread(delegate()
            {
                using (WebBrowser browser = new WebBrowser())
                {
                    browser.ScrollBarsEnabled = false;
                    browser.AllowNavigation = true;
                    browser.ScriptErrorsSuppressed = false;
                    browser.Navigate(url);
                    browser.Width = 1024;
                    browser.Height = 768;
                    browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
                    while (browser.ReadyState != WebBrowserReadyState.Complete)
                    {
                        System.Windows.Forms.Application.DoEvents();
                    }
                }
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            string s = html;
            return s;
        }

        private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser browser = sender as WebBrowser;
            html = browser.DocumentText;


            //using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height))
            //{
            //    browser.DrawToBitmap(bitmap, new Rectangle(0, 0, browser.Width, browser.Height));
            //    using (MemoryStream stream = new MemoryStream())
            //    {
            //        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            //        byte[] bytes = stream.ToArray();
            //        imgScreenShot.Visible = true;
            //        imgScreenShot.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
            //    }
            //}
        }

        //protected void GetWebPageWorker()
        //{
        //    using (WebBrowser browser = new WebBrowser())
        //    {
        //        //  browser.ClientSize = new Size(_width, _height);
        //        browser.ScrollBarsEnabled = false;
        //        browser.ScriptErrorsSuppressed = true;
        //        browser.Navigate(_url);

        //        // Wait for control to load page
        //        while (browser.ReadyState != WebBrowserReadyState.Complete)
        //        {
        //            Thread.Sleep(5000);
        //            Application.DoEvents();
        //        }
        //        Thread.Sleep(10000);
        //        Application.DoEvents();
        //        html = browser.DocumentText;
        //    }
        //}

    }
}